var mNavList;
var mIconList;
var curTypeId = 0;
var curRegion = '';

function init(){ 
	
	if(document.persisted) {
		return false;
	}
	
	refreshEpg();
	
	mNavList = new NavList({
		onclick:onclickType,
		onfocus:onfocusType,
	});
	mIconList = new IconList({
		formatHtml:formatItemHtml,
		onfocus:onfocusItem,
		onblur:onblurItem,
		onclick:onclickItem,
		emptyClass:'emptyClass',
		disNum:4,
		itemHeight:120,
		splitHeight:480,
	});
	
	initData(afterInitData);
}

function afterInitData(){
	mNavList.setDataList(typeList,true);	
	for(var i = 0;i<typeList.length;i++){
		mIconList.formatType(typeList[i]);
		
	}
	mNavList.setClick(1,true);
}

function onfocusType(type,tar){
	curRegion = 'nav';
}

function onclickType(type,tar){
	if(curTypeId!= tar){
		curTypeId = tar;
		mIconList.setType(type);
		
		var gTextBox = document.getElementById('gTextBox');
		gTextBox.innerHTML = type.name;
	}else{
		mIconList.setFocus();
	}
}

function onfocusItem(data,tar){
	curRegion = 'body';
	var box = document.getElementById(this.target+'-box-'+tar);
	box.className = '_iconListSel';
	
	var img = $.getImageUrl(data.icon,'images/tv_default.png'); 
	
	var tvIcon = document.getElementById('tvIcon');
	tvIcon.innerHTML = '<img src="'+img+'"/>';
	var tvTitle = document.getElementById('tvTitle');
	tvTitle.innerHTML = data.name;
	
	changeProgram(data);
}

function changeProgram(data){
	var con = '';
	var programList = getSession('p_'+data.id);
	if(programList){
		eval("var pList = "+programList);
		for(var i = 0;i<pList.propMap.program.length;i++){
			if(i>3)break;
			con += formatProgramHtml(pList.propMap.program[i]);
		}
	}
	if(!con){
		con += '<div class="epgItem"><div class="epgName2">Program Infomation Not Found.</div></div>';
	}
	var epgBox = document.getElementById('epgBox');
	epgBox.innerHTML = con;
}

//格式化节目单
function formatProgramHtml(program,tar){
	var reCat = /\d{1,2}:\d{1,2}/i;
	var beginTime,endTime,name;
	beginTime = reCat.exec(program.beginTime);
	endTime = reCat.exec(program.endTime);
	name = program.name;
	
	var con = '<div class="epgItem"><div class="epgTime">'+beginTime+'-'+endTime+'</div><div class="epgName">'+name+'</div></div>';
	return con;
}

function onblurItem(data,tar){
	var box = document.getElementById(this.target+'-box-'+tar);
	box.className = '_iconListBg';
}

function onclickItem(data,tar){
	gotoPlayPage(data,tar,curTypeId);	
}


function formatItemHtml(i,row,data){
	var con = '';
	var img = $.getImageUrl(data.icon,'images/tv_default.png'); 
	con += '<div class="_iconListIcon"><a href="#" id="'+this.target+'-a-'+i+'" tar="'+i+'" row="'+row+'"><img src="'+img+'"/></a></div>';
	con += '<div class="_iconListBg" id="'+this.target+'-box-'+i+'"> </div>';
	return con;
}


function gotoPlayPage(data,tar,curTypeId){
	var playUrl= 'play/index.html';
	setSession("live_itemId",tar+"");
	setSession("live_typeId",curTypeId+"");
	$.gotoPage(playUrl);
}
